package com.jidesoft.example;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.InputStream;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

import com.jidesoft.plaf.LookAndFeelFactory;
import com.jidesoft.swing.JideSwingUtilities;
import com.jidesoft.swing.JideTabbedPane;
import com.jidesoft.swing.Searchable;
import com.jidesoft.swing.SearchableBar;
import com.jidesoft.swing.SearchableUtils;

public class SearchableBarDemo {

	public SearchableBar _textAreaSearchableBar;

	public static void main(String[] args) {
		
		// 需要在SwingUtilities调用,不然搜索不会自动跳转
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				LookAndFeelFactory.installDefaultLookAndFeelAndExtension();

				JFrame frame = new JFrame("Test");
				frame.addWindowListener(new WindowAdapter() {
					public void windowClosing(WindowEvent e) {
						System.out.println("关闭");
					}
				});
				frame.add(new SearchableBarDemo().getDemoPanel());
				// 调用此方法使组件布局生效
				frame.pack();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				JideSwingUtilities.globalCenterWindow(frame);
				frame.toFront();
				frame.setVisible(true);
			}
		});
	}

	public Component getDemoPanel() {
		JideTabbedPane tabbedPane = new JideTabbedPane();
		tabbedPane.addTab("JTextArea with SearchableBar",
				createSearchableTextArea());
		return tabbedPane;
	}

	private JPanel createSearchableTextArea() {
		final JTextComponent textArea = SearchableBarDemo.createEditor("Readme.txt");
		final JPanel panel = new JPanel(new BorderLayout());
		panel.add(new JScrollPane(textArea), BorderLayout.CENTER);
		
		Searchable searchable = SearchableUtils.installSearchable(textArea);
		searchable.setRepeats(true);
		
		_textAreaSearchableBar = SearchableBar.install(searchable,
				KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_DOWN_MASK),
				new SearchableBar.Installer() {
					public void openSearchBar(SearchableBar searchableBar) {
						String selectedText = textArea.getSelectedText();
						if (selectedText != null && selectedText.length() > 0) {
							searchableBar.setSearchingText(selectedText);
						}
						panel.add(searchableBar, BorderLayout.AFTER_LAST_LINE);
						panel.invalidate();
						panel.revalidate();
					}

					public void closeSearchBar(SearchableBar searchableBar) {
						panel.remove(searchableBar);
						panel.invalidate();
						panel.revalidate();
					}
				});
		_textAreaSearchableBar.getInstaller().openSearchBar(_textAreaSearchableBar);
		_textAreaSearchableBar.setShowMatchCount(true);
		_textAreaSearchableBar.setHighlightAll(true);
		_textAreaSearchableBar.setSearchingText("2000");
		
		return panel;
	}

	public static JTextComponent createEditor(String fileName) {
		JTextComponent textComponent = new JTextArea() {
			@Override
			public Dimension getPreferredScrollableViewportSize() {
				return new Dimension(700, 400);
			}
		};
		Document doc = new DefaultStyledDocument();
		try {
			// 开始读取
			InputStream in = SearchableBarDemo.class
					.getResourceAsStream(fileName);
			if (in != null) {
				byte[] buff = new byte[4096];
				int nch;
				while ((nch = in.read(buff, 0, buff.length)) != -1) {
					doc.insertString(doc.getLength(), new String(buff, 0, nch),
							null);
				}
				textComponent.setDocument(doc);
			} else {
				textComponent
						.setText("Copy readme.txt into the class output directory");
			}
		} catch (Exception e) {
			// 忽略
		}
		return textComponent;
	}

}
